<script setup>
import {onMounted, ref} from "vue";
import {
  deleteFromCartApi, getContactApi,
  getUserBoughtProducts,
  getUserCartProducts,
  purchaseProduct
} from "@/js/apiHelper";
import {timeCorrect} from "@/js/jsHelper";

onMounted(()=>{
  appendProducts1()
  appendProducts2()
})
/**
 * 购物车中的商品列表
 * @type {Ref<UnwrapRef<*[]>>}
 */
const products1 = ref([])
const appendProducts1 = ()=>{
  const productsResult = getUserCartProducts()
  productsResult.then((res) => {
    if (res.data.length <= 0){
      alert('没有更多数据了')
      return
    }
    for (const datum of res.data) {
      datum.publishTime = timeCorrect(datum.publishTime)
      products1.value.push(datum)
    }
  }).catch(e=>{
    console.log('获取失败'+e)
  })
}
//直接购买
const contactSeller = (productId)=>{
  const result = purchaseProduct(productId)
  result.then(res=>{
    console.log(res)
    if (res.data === 0){
      alert('购买成功')
      products1.value = products1.value.filter((item) => item.productId !== productId)
      appendProducts2()
    }else{
      switch (res.data){
        case -1:
          alert('未知错误')
          break;
        case -2:
          alert('手慢了，商品已被购买！')
          break;
        case -3:
          alert('不能购买自己的商品！')
          break;
      }
    }
  })
}
//移出购物车
const deleteFromCart = (productId)=>{
  const result = deleteFromCartApi(productId)
  result.then(res=>{
    if (res.data){
      alert('移出成功')
      products1.value = products1.value.filter((item) => item.productId !== productId)
    }
  })
}
//全部购买
const cleanCart = async () => {
  for (const products1Element of products1.value) {
    await contactSeller(products1Element.productId);
  }
  alert("已清空购物车！")
  refreshAll2()
}

/**
 * 我买到的商品列表
 */
const products2 = ref([])
const appendProducts2 = ()=>{
  const productsResult = getUserBoughtProducts()
  productsResult.then((res) => {
    if (res.data.length <= 0){
      alert('没有更多数据了')
      return
    }
    for (const datum of res.data) {
      datum.publishTime = timeCorrect(datum.publishTime)
      products2.value.push(datum)
    }
  }).catch(e=>{
    console.log('获取失败'+e)
  })
}
//刷新
const refreshAll2 = ()=>{
  products2.value = []
  setTimeout(()=>{
    appendProducts2()
  },200)
}
//联系卖家
const contactWithSeller = (productId)=>{
  const result = getContactApi(productId)
  result.then(res=>{
    alert('卖家电话号码：'+res.data.ownerPhone)
  })
}

</script>

<template>
  <div>
    <!--购物车中-->
    <div class="cart-container">
      <h1>购物车列表</h1>
      <!--刷新-->
      <div class="refresh-btn" @click="cleanCart">一键购买</div>
      <!--列表-->
      <div class="inside-container scroll-container">
        <div v-for="(item, index) in products1" :key="index" class="procuct-container">
          <!--背景-->
          <div class="block-back">
            <img :src="item.productImgs">
            <div></div>
          </div>
          <!--图片-->
          <div class="block-img">
            <img :src="item.productImgs">
          </div>
          <!--基础信息容器-->
          <div class="block-basic">
            <h2>{{ item.productPrice }}</h2>
            <h3>{{ item.productTitle }}</h3>
            <h4>发布时间：{{ item.publishTime }}</h4>
            <h4>地址：{{ item.location }}</h4>
            <button v-if="item.status === 1" @click="contactSeller(item.productId)">直接购买</button>
            <button v-else style="background: linear-gradient(to right bottom, #ccc, #e1d0ce);">已售出</button>
            <button @click="deleteFromCart(item.productId)">移出购物车</button>
          </div>
          <!--介绍容器-->
          <div class="block-intro">
            {{ item.productIntro }}
          </div>
        </div>
      </div>
    </div>
    <!--我买到的-->
    <div class="bought-container">
      <h1>我买到的</h1>
      <!--刷新-->
      <div class="refresh-btn" @click="refreshAll2">刷新</div>
      <!--列表-->
      <div class="inside-container scroll-container">
        <div v-for="(item, index) in products2" :key="index" class="procuct-container">
          <!--背景-->
          <div class="block-back">
            <img :src="item.productImgs">
            <div></div>
          </div>
          <!--图片-->
          <div class="block-img">
            <img :src="item.productImgs">
          </div>
          <!--基础信息容器-->
          <div class="block-basic">
            <h2>{{ item.productPrice }}</h2>
            <h3>{{ item.productTitle }}</h3>
            <h4>购买时间：{{ item.publishTime }}</h4>
            <h4>地址：{{ item.location }}</h4>
            <button @click="contactWithSeller(item.productId)">联系卖家</button>
          </div>
          <!--介绍容器-->
          <div class="block-intro">
            {{ item.productIntro }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
  /*共同*/
  h1{
    font-size: 18px;
    font-weight: bold;
    letter-spacing: 2px;
    color: #383838;
  }
  .inside-container{
    position: absolute;
    top: 32px;
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 8px;
    overflow-y: scroll;
    overflow-x: hidden;
  }
  .refresh-btn{
    position: absolute;
    right: 8px;
    top: 8px;
    font-size: 14px;
    color: #383838;
    cursor: pointer;
    text-decoration: underline;
  }
  .scroll-container{
    overflow-y: scroll;
    overflow-x: hidden;
    padding-right: 4px;
  }
  .scroll-container .procuct-container{
    width: 100%;
    margin-bottom: 12px;
    height: 460px;
    border-radius: 16px;
    box-shadow: 2px 2px 6px #00000033;
    overflow: hidden;
    margin-right: 4px;
    position: relative;
    min-width: 600px;
  }
  /*背景*/
  .scroll-container .procuct-container .block-back{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
  .scroll-container .procuct-container .block-back div{
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #fafafaaa;
    backdrop-filter: blur(4px);
  }
  .scroll-container .procuct-container .block-back img{
    min-width: 100%;
    min-height: 100%;
    position: absolute;
    object-fit: cover;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  /*图片*/
  .scroll-container .procuct-container .block-img{
    position: absolute;
    left: 12px;
    top: 12px;
    height: 270px;
    width: 360px;
    background-color: #fafafa;
    border-radius: 4px;
    border: #eee solid 2px;
    box-shadow: inset 0 0 8px #00000033;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
  }
  .scroll-container .procuct-container .block-img img{
    max-height: 100%;
    max-width: 100%;
    position: absolute;
    object-fit: contain;
  }
  /*基础信息*/
  .scroll-container .procuct-container .block-basic{
    position: absolute;
    left: 386px;
    right: 12px;
    top: 12px;
    height: 270px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }
  .scroll-container .procuct-container .block-basic h2{
    font-weight: bold;
    color: #5471c0;
    font-size: 42px;
    display: flex;
    align-items: start;
    margin-bottom: 0;
    position: relative;
  }
  .scroll-container .procuct-container .block-basic h2::after{
    content: "元";
    font-size: 22px;
    transform: translate(4px,14px);
  }
  .scroll-container .procuct-container .block-basic h3{
    font-size: 30px;
    text-overflow: clip;
    max-width: 100%;
    white-space: nowrap;
    font-weight: bold;
    color: #383838;
  }
  .scroll-container .procuct-container .block-basic h4{
    font-size: 16px;
    white-space: pre-line;
    max-width: 100%;
  }
  .scroll-container .procuct-container .block-basic button{
    padding: 8px 24px;
    outline: none;
    border: none;
    box-shadow: 2px 2px 4px #00000033;
    background: linear-gradient(to right bottom, #9bafe5, #bba1e3);
    color: #fff;
    cursor: pointer;
    border-radius: 4px;
    transition: .4s;
  }
  .scroll-container .procuct-container .block-basic button:hover{
    transform: rotateX(16deg);
    box-shadow: 4px 4px 12px #799be588;
    transition: .4s;
  }

  /*介绍信息*/
  .scroll-container .procuct-container .block-intro{
    position: absolute;
    top: 296px;
    bottom: 12px;
    left: 12px;
    right: 12px;
    padding: 8px;
    white-space: pre-line;
    overflow:scroll;
  }
  /*更多*/
  .more-btn{
    width: 100%;
    text-align: center;
    cursor: pointer;
  }
  /*购物车中*/
  .cart-container{
    position: absolute;
    left: 12px;
    top: 12px;
    right: calc(50% + 8px);
    bottom: 12px;
  }
  /*我买到的*/
  .bought-container{
    position: absolute;
    right: 12px;
    top: 12px;
    left: calc(50% + 8px);
    bottom: 12px;
  }


</style>